﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.Win32;

namespace RSHTreelReset
{
    sealed class FreeEvaluationInfo
    {
        public static readonly DateTime INSTALLER_TIME = new DateTime(1703, 5, 29);
        public static readonly string ROOT_GUID = "{9656c84c-e0b4-4454-996d-977eabdf9e86}";
        public static readonly string ROOT_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Ext\\Settings\\" + FreeEvaluationInfo.ROOT_GUID;
        private const char _separator = '@';
        public bool IsFeedbackRequested;
        public DateTime RecentExipartionNotification;
        public DateTime TrialStart;

        public bool IsNotStarted
        {
            get
            {
                return this.TrialStart == FreeEvaluationInfo.INSTALLER_TIME;
            }
        }

        static FreeEvaluationInfo()
        {
        }

        public FreeEvaluationInfo(DateTime trialStart, bool isFeedbackRequested, DateTime recentExipartionNotification)
        {
            this.TrialStart = trialStart;
            this.IsFeedbackRequested = isFeedbackRequested;
            this.RecentExipartionNotification = recentExipartionNotification;
        }

        protected string GetRegistryString()
        {
            return EncryptionUtil.Encrypt(string.Format("{0}{3}{1}{3}{2}", (object)this.TrialStart.ToString((IFormatProvider)DateTimeFormatInfo.InvariantInfo), (object)(this.IsFeedbackRequested ? 1 : 0), (object)this.RecentExipartionNotification.ToString((IFormatProvider)DateTimeFormatInfo.InvariantInfo), (object)'@'));
        }

        public static FreeEvaluationInfo CreateInfo(string registryValue)
        {
            if (registryValue == null)
                return (FreeEvaluationInfo)null;
            string[] strArray = EncryptionUtil.Decrypt(registryValue).Split(new char[1]
      {
        '@'
      });
            DateTime result1;
            bool result2;
            if (strArray.Length != 3 || !DateTime.TryParse(strArray[0], (IFormatProvider)DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out result1) || !bool.TryParse(strArray[1], out result2))
            {
                return (FreeEvaluationInfo)null;
            }
            else
            {
                DateTime result3;
                return !DateTime.TryParse(strArray[2], (IFormatProvider)DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out result3) ? (FreeEvaluationInfo)null : new FreeEvaluationInfo(result1, result2, result3);
            }
        }

        public static FreeEvaluationInfo GetFreeTrialInfo(string productGuid, string rootKeyName)
        {
            FreeEvaluationInfo result = (FreeEvaluationInfo)null;
            FreeEvaluationInfo.GetTrialRootKey(false, (FreeEvaluationInfo.TrialRootKeyDelegate)(rootKey =>
            {
                try
                {
                    result = FreeEvaluationInfo.CreateInfo(rootKey.GetValue(productGuid) as string);
                    return result != null;
                }
                catch
                {
                    return false;
                }
            }), rootKeyName);
            return result;
        }

        public static void SetFreeTrialInfo(string productGuid, FreeEvaluationInfo info, string rootKeyName)
        {
            FreeEvaluationInfo.GetTrialRootKey(true, (FreeEvaluationInfo.TrialRootKeyDelegate)(rootKey =>
            {
                try
                {
                    rootKey.SetValue(productGuid, (object)info.GetRegistryString());
                    return true;
                }
                catch (Exception exception_0)
                {
                    Console.WriteLine((object)exception_0);
                }
                return false;
            }), rootKeyName);
        }

        protected static bool GetTrialRootKey(bool forWrite, FreeEvaluationInfo.TrialRootKeyDelegate @do, string rootKeyName)
        {
            RegistryKey[] registryKeyArray = new RegistryKey[2]
      {
        Registry.CurrentUser,
        Registry.LocalMachine
      };
            foreach (RegistryKey registryKey in registryKeyArray)
            {
                RegistryKey rootKey = (RegistryKey)null;
                try
                {
                    rootKey = forWrite ? registryKey.CreateSubKey(rootKeyName) : registryKey.OpenSubKey(rootKeyName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine((object)ex);
                }
                if (rootKey != null)
                {
                    using (rootKey)
                    {
                        if (@do(rootKey))
                            return true;
                    }
                }
            }
            return false;
        }

        protected delegate bool TrialRootKeyDelegate(RegistryKey rootKey);
    }
}

